package com.wuwei.elearning.messageQueen;

import com.wuwei.elearning.config.MinioConfig;
import com.wuwei.elearning.dto.ConversionMessage;
import com.wuwei.elearning.enumPackage.Constant;
import com.wuwei.elearning.question.domain.Video;
import com.wuwei.elearning.question.service.VideoService;
import com.wuwei.elearning.service.CacheService;
import com.wuwei.elearning.utils.DocumentService;
import com.wuwei.elearning.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.concurrent.TimeUnit;

import static com.wuwei.elearning.utils.FileUtils.extractVideoDuration;

/**
 * A simple implementation of MessageConsumer
 */
@Slf4j
@Component
public class VideoMessageConsumer implements MessageConsumer {
    private String topic = Constant.VIDEO_CONVERSION_TOPIC;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private DocumentService documentService;

    @Resource
    private CacheService cacheService;

    @Resource
    private MinioConfig minioConfig;

    /**
     * Default constructor for Spring to use
     */
    public VideoMessageConsumer() {
        // Default constructor for Spring
    }
    
    @Override
    public boolean consume(ConversionMessage conversionMessage) {
        Message message = (Message)conversionMessage.getPayload();
        String fileName = message.getFileName();
        Long id = message.getId();
        String minioPath = message.getMinioPath();
        try {
            log.info("接收到转换任务: {} - {}", fileName, System.currentTimeMillis());
            File tempInputFile = new File(fileName);
            minioUtil.downloadFile(minioPath, tempInputFile);
            try {
                long duration = extractVideoDuration(tempInputFile);
                String previewUrl = minioUtil.getPreviewUrl(minioPath);
                previewUrl = minioConfig.getEndpoint() + previewUrl;
                String upload = Constant.VIDEO_DIR + "/" + fileName;
                String redisKey = Constant.MINIO_VIDEO + upload;
                cacheService.setEx(redisKey, previewUrl, 6, TimeUnit.DAYS);

                Video video = videoService.getById(id);
                video.setUrl(upload);
                video.setDuration(duration);
                videoService.updateById(video);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("视频长度提取失败-{}",fileName);
                throw new RuntimeException(e);
            }
            finally {
                // 清理临时文件
                if (tempInputFile.exists()) {
                    tempInputFile.delete();
                }
            }
        } catch (Exception e) {
            videoService.removeById(id);
            log.error("视频长度提取失败，删除数据库数据-{}",fileName);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return true;
    }

    @Resource
    private VideoService videoService;


    @Override
    public String getTopic() {
        return topic;
    }
} 